package gov.nhso.prov.acc.apps.acc6;

import gov.nhso.prov.acc.beans.Acc6AllocateBean;
import gov.nhso.prov.acc.beans.Acc6IncomeBean;
import gov.nhso.prov.acc.beans.Acc6TransactionBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.Utility;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;
import com.isud.paging.PageBean;

@SuppressWarnings("rawtypes")
public class ACC60203 extends Utility{
	public String url = "/apps/acc6/ACC60203A.jsp";

	public ACC60203(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = this.getAttr(request, "job");
		if(job.equals("init")){
			this.clearLastRequest(request, "lastSearch");
			this.init(request);
		}else if(job.equals("search")){
			this.setLastRequest(request, "lastSearch");
			this.search(request);
		}else if(job.equals("add")){
			this.add(request);
		}else if(job.equals("save")){
			this.save(request);
		}else if(job.equals("viewUpdate")){
			this.viewUpdate(request);
		}else if(job.equals("edit")){
			this.edit(request);
		}else if(job.equals("delete")){
			this.delete(request);
		}else if(job.equals("back")){
			if (this.getLastRequest(request, "lastSearch")) {
				this.search(request);
			} else {
				this.init(request);
			}
		}else if(job.equals("confirm")){
			this.confirm(request);
		}
	}
	private String init(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			
			request.setAttribute("listBudgetYear", this.dataUtil(dao).lsBudgetYear());
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String search(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			PageBean pageBean = new PageBean();
			Acc6IncomeBean incomeBean = new Acc6IncomeBean();
			this.beanUtil().copyRequest2Bean(request, incomeBean);
			String startDate = this.getAttr(request, "startDate");
			String endDate = this.getAttr(request, "endDate");
			String condition = "";
			
			if(!dao.chkData(startDate).equals("")){
				startDate = this.dateUtil().changeFormatDate(startDate, "/", "/");
				condition += " AND TRUNC(INCOME_DATE) >= TO_DATE('"+startDate+"','DD/MM/YYYY') ";
			}			
			if(!dao.chkData(endDate).equals("")){
				endDate = this.dateUtil().changeFormatDate(endDate, "/", "/");
				condition += " AND TRUNC(INCOME_DATE) <= TO_DATE('"+endDate+"','DD/MM/YYYY') ";
			}
			incomeBean.setActive("Y");
			incomeBean.setIncomeType("F");
			this.beanUtil().copyRequest2Bean(request, pageBean);
			if (this.user(request).getAreaLevel().equals("2")){	
				condition = " AND PROVINCE_ID IN (SELECT PROVINCE_ID FROM PROVINCE WHERE NHSO_ZONE = '" + this.user(request).getAreaCode()+ "' ) ";
			}
			pageBean = dao.selectPage(incomeBean,condition, pageBean);
			
			if (pageBean.getPageData() == null) {
				this.message(request).setMsgCode("I001");
			}
			request.setAttribute("pageBean", pageBean);
			request.setAttribute("listBudgetYear", this.dataUtil(dao).lsBudgetYear());
			request.setAttribute("provinceId", incomeBean.getProvinceId());
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}

	private String add(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			Acc6IncomeBean incomeBean = new Acc6IncomeBean();
			//Acc6AllocateBean allocateBean = new Acc6AllocateBean();
			//List listAllocate = null;
			
			//allocateBean.setAllocateApply("Y");
			//allocateBean.setActive("Y");
			//listAllocate = dao.selectList(allocateBean, "AND ALLOCATE_CODE LIKE '%" + this.ioc + "' ");
			//listAllocate = dao.selectList(allocateBean);
			
			request.setAttribute("provinceId", incomeBean.getProvinceId());
			incomeBean.setDocNo("สร้างจากระบบ");
			request.setAttribute("incomeBean", incomeBean);
			//request.setAttribute("listAllocate", listAllocate);
			request.setAttribute("isEdit", "N");
			request.setAttribute("listBudgetYear", this.dataUtil(dao).lsBudgetYear());
			request.setAttribute("now", this.dateUtil().getThaiCurrentDate());
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc6/ACC60203B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String save(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		boolean success = false;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			Acc6IncomeBean incomeBean = new Acc6IncomeBean();
			this.beanUtil().copyRequest2Bean(request, incomeBean);
			String docNo = this.genCode(dao).getIncomeDocNo(incomeBean.getBudgetYear(), incomeBean.getProvinceId(), "F");
			
			incomeBean.setDocNo(docNo);
			incomeBean.setIncomeAmount(incomeBean.getIncomeAmount().replace(",", ""));
			//incomeBean.setAllocateCode(incomeBean.getBudgetYear().substring(2) + this.ioc);
			if(dao.chkData(incomeBean.getIncomeDesc()).equals("")){
				incomeBean.setIncomeDesc("null");
			}
			incomeBean.setIncomeType("F");
			incomeBean.setIncomeDate(this.dateUtil().changeDateToDB(incomeBean.getIncomeDate()));
			incomeBean.setUpdatedBy(this.user(request).getUserId());
			incomeBean.setCreatedBy(this.user(request).getUserId());
			success = dao.insert(incomeBean);
			if(success){
				this.message(request).setMsgCode("I002");
				request.setAttribute("documentNo", docNo);
				request.setAttribute("isEdit", "Y");
			}else{
				this.message(request).setMsgCode("W001");
			}
			conn.commit();
			
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				if (success) {
					this.viewUpdate(request);
				} else {
					this.url = "/apps/acc6/ACC60203B.jsp";
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String viewUpdate(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			Acc6IncomeBean incomeBean = new Acc6IncomeBean();
			Acc6AllocateBean allocateBean = new Acc6AllocateBean();
			List listAllocate = null;
			
			String documentNo = this.getAttr(request, "documentNo");
			String provinceId = this.getAttr(request, "provinceId");
			String budgetYear = this.getAttr(request, "budgetYear");
			String isEdit = this.getAttr(request, "isEdit");
			allocateBean.setAllocateApply("Y");
			allocateBean.setActive("Y");
			allocateBean.setBudgetYear(budgetYear);
			listAllocate = dao.selectList(allocateBean);
			if(isEdit.equals("") || isEdit == null){
				isEdit = "N";
			}
			incomeBean.setDocNo(documentNo);
			incomeBean.setProvinceId(provinceId);
			incomeBean.setBudgetYear(budgetYear);
			incomeBean = (Acc6IncomeBean) dao.select(incomeBean);

			request.setAttribute("provinceId", incomeBean.getProvinceId());
			request.setAttribute("provinceName", this.dataUtil(dao).getProvinceName(incomeBean.getProvinceId()));
			request.setAttribute("incomeBean", incomeBean);
			request.setAttribute("isEdit", isEdit);
			request.setAttribute("listAllocate", listAllocate);
			request.setAttribute("listBudgetYear", this.dataUtil(dao).lsBudgetYear());
			

			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			this.url = "/apps/acc6/ACC60203B.jsp";
		}
		return this.url;
	}
	private String edit(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		boolean success = false;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			Acc6IncomeBean incomeBean = new Acc6IncomeBean();
			this.beanUtil().copyRequest2Bean(request, incomeBean);
			
			incomeBean.setIncomeDate(this.dateUtil().changeDateToDB(incomeBean.getIncomeDate()));
			//incomeBean.setAllocateCode(incomeBean.getBudgetYear().substring(2) + this.ioc);
			if(dao.chkData(incomeBean.getIncomeDesc()).equals("")){
				incomeBean.setIncomeDesc("null");
			}
			incomeBean.setIncomeAmount(incomeBean.getIncomeAmount().replace(",", ""));
			incomeBean.setUpdatedBy(this.user(request).getUserId());
			incomeBean.setUpdatedDate(this.sysDate);

			success = dao.update(incomeBean);
			if(success){
				this.message(request).setMsgCode("I002");
				request.setAttribute("documentNo", incomeBean.getDocNo());
				request.setAttribute("isEdit", "Y");
			}else{
				this.message(request).setMsgCode("W001");
			}
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				if (success) {
					this.viewUpdate(request);
				} else {
					this.url = "/apps/acc6/ACC60203B.jsp";
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String delete(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		boolean success = false;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			Acc6IncomeBean incomeBean = new Acc6IncomeBean();
			String budgetYear = this.getAttr(request, "budgetYear");
			String docNo = this.getAttr(request, "documentNo");
			
			incomeBean.setDocNo(docNo);
			incomeBean.setActive("N");
			incomeBean.setUpdatedBy(this.user(request).getUserId());
			incomeBean.setUpdatedDate(this.sysDate);
			success = dao.update(incomeBean);
			if(success){
				this.message(request).setMsgCode("I003");
				request.setAttribute("docNo", docNo);
				request.setAttribute("budgetYear", budgetYear);
			}else{
				this.message(request).setMsgCode("W001");
			}
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				if (success) {
					this.search(request);
				} else {
					this.url = "/apps/acc6/ACC60203A.jsp";
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String confirm(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		boolean success = false;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			String ts = "";

			Acc6IncomeBean incomeBean = new Acc6IncomeBean();
			Acc6IncomeBean bean = new Acc6IncomeBean();
			Acc6TransactionBean transactionBean = new Acc6TransactionBean();
			this.beanUtil().copyRequest2Bean(request, incomeBean);
			String incomeDate = "";

			bean.setDocNo(incomeBean.getDocNo());
			bean.setProvinceId(incomeBean.getProvinceId());
			bean.setBudgetYear(incomeBean.getBudgetYear());
			dao.select(bean);

			if (bean.getIncomeStatus().equals("P")) {
				ts = this.dateUtil().getCurrentTimeStamp2DB();
				incomeDate = this.dateUtil().changeDateToDB(incomeBean.getIncomeDate());
				incomeBean.setTransactionTimestamp(this.dateUtil().setTimeStamp2DB(ts));
				incomeBean.setIncomeStatus("Y");
				if(dao.chkData(incomeBean.getIncomeDesc()).equals("")){
					incomeBean.setIncomeDesc("null");
				}
				incomeBean.setIncomeAmount(incomeBean.getIncomeAmount().replace(",", ""));
				incomeBean.setIncomeDate(incomeDate);
				incomeBean.setUpdatedBy(this.user(request).getUserId());
				incomeBean.setUpdatedDate(this.sysDate);
				success = dao.update(incomeBean);
				
				transactionBean.setTransactionTimestamp(this.dateUtil().setTimeStamp2DB(ts));
				transactionBean.setTransactionType("F");
				transactionBean.setAllocateCode(incomeBean.getAllocateCode());
				transactionBean.setRefNo(incomeBean.getRefNo());
				transactionBean.setAllocateCode(incomeBean.getAllocateCode());
				transactionBean.setBudgetYear(incomeBean.getBudgetYear());
				transactionBean.setProvinceId(incomeBean.getProvinceId());
				transactionBean.setIncomeDate(incomeDate);
				transactionBean.setIncomeAmount(incomeBean.getIncomeAmount());
				transactionBean.setTransactionType("F");
				transactionBean.setReasonCode("00");
				dao.insert(transactionBean);
				
				if(success){
					this.message(request).setMsgCode("I002");
					request.setAttribute("documentNo", incomeBean.getDocNo());
					request.setAttribute("isEdit", "V");
				}else{
					this.message(request).setMsgCode("W001");
				}
			} else {
				this.viewUpdate(request);
				this.message(request).setMsg("E", "ไม่สามารถยืนยันได้ สถานะไม่ได้อยู่เตรียมดำเนินการ กรุณาตรวจสอบ");
			}
			
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				if (success) {
					this.viewUpdate(request);
				} else {
					this.url = "/apps/acc6/ACC60203B.jsp";
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}

}
